package com.devunion.reader.client.panel;

import com.devunion.reader.client.core.AbstractComposite;
import com.devunion.reader.client.to.SearchProjectTo;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.binding.Bindings;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.SimpleComboBoxFieldBinding;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;

/**
 * @author Timoshenko Alexander 22.08.2009 : 14:33:44
 */
public class SimpleSearchPanel extends AbstractComposite implements Bindable<SearchProjectTo> {
    private SimpleComboBox<String> annotationType = new SimpleComboBox<String>();
    private TextField annotationText = new TextField();

    private Button searchButton;
    protected Bindings bindings;

    public void initialize() {
        setLayout(new ColumnLayout());
        setFrame(true);
        setScrollMode(Style.Scroll.AUTOY);

        FieldSet annotationFieldSet = new FieldSet();
        annotationFieldSet.setLayout(new FormLayout(FormPanel.LabelAlign.TOP));
        annotationFieldSet.setHeading("Annotation");
        annotationType.add("TEXT");
        annotationType.add("IMAGE");
        annotationType.setFieldLabel("Annotation Type");
        annotationType.setEmptyText("Select annotations type");
        annotationFieldSet.add(annotationType);
        annotationText.setFieldLabel("Annotation Text");
        annotationText.setEmptyText("Enter annotations text");
        annotationFieldSet.add(annotationText);
        add(annotationFieldSet, new ColumnData(.2));

        searchButton = new Button("Search");
        setButtonAlign(Style.HorizontalAlignment.LEFT);
        addButton(searchButton);
        addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {
            public void componentSelected(ButtonEvent buttonEvent) {
                reset();
            }
        }));
    }

    public void bind(SearchProjectTo form) {
        if (bindings == null) {
            bindings = new Bindings();
        } else {
            bindings.unbind();
        }

        bindings.addFieldBinding(new SimpleComboBoxFieldBinding(annotationType, SearchProjectTo.ANNOTATION_TYPE));
        bindings.addFieldBinding(new FieldBinding(annotationText, SearchProjectTo.ANNOTATION_TEXT));

        bindings.bind(form);
    }

    public void reset() {
        annotationType.reset();
        annotationText.reset();
    }

    public Button getSearchButton() {
        return searchButton;
    }
}